by Philip Decker (1 Submission)
Category: Windows API Call/Explanation
Compatability: Visual Basic 3.0
Difficulty: Intermediate
Date Added: Wed 3rd February 2021
Rating: (4 Votes)
This code will return the freespace on a drive even if it exceeds 2GB.
Inputs
Drive ie. "C:"
Assumes
I saw lots of calculations for drive sizes but this one works for me and does not require calculations at all. It will return what Windows shows under properties on a drive. It even shows mapped network drives properly. Hope this code helps someone.
Code Returns
Drive size.
Side Effects
None.
API DeclarationsDim FB, BT, FBT As Currency
Dim DriveSize As String
Const Gigabyte = 1073741824
Const Megabyte = 1048576
Dim retval As Long
Private Declare Function GetDiskFreeSpace_FAT32 _
Lib "kernel32" Alias "GetDiskFreeSpaceExA" _
(ByVal lpRootPathName As String, _
FreeBytesToCaller As Currency, BytesTotal _
As Currency, FreeBytesTotal As Currency) _
As Long
Public Function GetDriveInfo(DriveName As String) As String
retval = GetDiskFreeSpace_FAT32(Left(DriveName, 2), FB, BT, FBT)
FBT = FBT * 10000 'convert result to actual size in bytes
If FBT / Gigabyte < 1 Then 'If less than 1GB then show as MB
DriveSize = Format(FBT / Megabyte, "####,###,###") & " MB free"
Else 'Show as GB
DriveSize = Format(FBT / Gigabyte, "####,###,###.00") & " GB free"
End If
GetDriveInfo = "[" & DriveSize & "]"
End Function